package com.lnj.messages.utiles;

import com.lnj.utils.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
public class UserBehaviorUtil{
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    public UserBehaviorUtil(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
    // 获取商品的浏览记录
    public Set<Long> getRecentViews(Long productId) {
        String key = RedisKeys.PRODUCT_RECOMMEND + productId;
        Set<String> items = stringRedisTemplate.opsForSet().members(key);
        return  items.stream().map(Long::valueOf).collect(Collectors.toSet());
    }

    //获取商品的购买记录
    public Set<Long> getRecentPurchases(Long productId) {
        String key = RedisKeys.PRODUCT_PAY + productId;
        Set<String> items = stringRedisTemplate.opsForSet().members(key);
        return  items.stream().map(Long::valueOf).collect(Collectors.toSet());
    }

    //获取商品的收藏记录
    public Set<Long> getRecentFavorites(Long productId) {
        String key = RedisKeys.PRODUCT_CART + productId;
        Set<String> items = stringRedisTemplate.opsForSet().members(key);
        return  items.stream().map(Long::valueOf).collect(Collectors.toSet());
    }

    /**
     * 根据商品ID列表获取所有用户行为，并将相同用户ID和商品ID的行为合并
     * @param productIds 商品ID列表
     * @return 合并后的用户行为列表
     */
    public List<ProductUserBehavior> getAllUserBehaviors(List<Long> productIds) {
        // 使用ConcurrentHashMap来存储用户行为，避免重复
        Map<String, ProductUserBehavior> behaviorMap = new ConcurrentHashMap<>();

        for (Long productId : productIds) {
            // 获取商品的浏览记录
            Set<Long> viewUsers = getRecentViews(productId);
            for (Long userId : viewUsers) {
                String key = userId + ":" + productId;
                ProductUserBehavior behavior = behaviorMap.computeIfAbsent(key,
                        k -> new ProductUserBehavior(userId, productId));
                behavior.setView(true);
            }

            // 获取商品的购买记录
            Set<Long> purchaseUsers = getRecentPurchases(productId);
            for (Long userId : purchaseUsers) {
                String key = userId + ":" + productId;
                ProductUserBehavior behavior = behaviorMap.computeIfAbsent(key,
                        k -> new ProductUserBehavior(userId, productId));
                behavior.setPurchase(true);
            }

            // 获取商品的收藏记录
            Set<Long> favoriteUsers = getRecentFavorites(productId);
            for (Long userId : favoriteUsers) {
                String key = userId + ":" + productId;
                ProductUserBehavior behavior = behaviorMap.computeIfAbsent(key,
                        k -> new ProductUserBehavior(userId, productId));
                behavior.setFavorite(true);
            }
        }

        // 将Map中的值转换为List并返回
        return new ArrayList<>(behaviorMap.values());
    }


}
